/*
 * GSS1.cxx
 *
 *  Created on: 11:55:15 AM Oct 30, 2012
 *      Author: Pallab
 * "I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
	double endtime = clock() + (seconds * CLOCKS_PER_SEC);
	while (clock() < endtime) {
		;
	}
}
#define T int
inline T fastIn() {
	register char c = 0;
	register T a = 0;
	bool neg = false;
	while (c < 33)
		c = getchar();
	while (c > 33) {
		if (c == '-') {
			neg = true;
		} else {
			a = (a * 10) + (c - '0');
		}
		c = getchar();
	}
	return neg ? -a : a;
}
inline void read();
inline void proc();
const int MX = 50010;
int number_count;
int numbers[MX];
struct Node {
	int total_sum;
	int pre;
	int suf;
	int bst;
	Node() {
		total_sum = pre = suf = bst = 0;
	}
	Node(int v) :
			total_sum(v), pre(v), suf(v), bst(v) {
	}
	void print() {
		cout << total_sum << ' ' << pre << ' ' << suf << ' ' << bst << "\n";
		puts("------");
	}
};
inline Node Combine(Node L, Node R) {
	Node aux;
	aux.total_sum = L.total_sum + R.total_sum;
	aux.bst = max(max(L.bst, R.bst), L.suf + R.pre);
	aux.pre = max(L.pre, L.total_sum + R.pre);
	aux.suf = max(R.suf, R.total_sum + L.suf);
	return aux;
}
Node t[MX * 4];
void build(int v, int l, int r) {
	if (l == r) {
		t[v] = Node(numbers[l]);
		/*cout<<v<<' '<<l<<' '<<r<<" => ";
		 t[v].print();*/
	} else {
		int v1 = v * 2, v2 = v * 2 + 1;
		int tm = (l + r) / 2;
		build(v1, l, tm);
		build(v2, tm + 1, r);
		t[v] = Combine(t[v1], t[v2]);
		/*cout<<v<<' '<<l<<' '<<r<<" =l> ";
		 t[v].print();*/
	}
}
Node Query(int v, int tl, int tr, int l, int r) {
	if (tl == l && tr == r) {
		return t[v];
	}
	int m = (tl + tr) / 2;
	if (r <= m)
		return Query(2 * v, tl, m, l, r);
	if (l > m)
		return Query(2 * v + 1, m + 1, tr, l, r);
	Node Left = Query(2 * v, tl, m, l, m);
	Node Right = Query(2 * v + 1, m + 1, tr, m + 1, r);
	return Combine(Left, Right);

}
int main() {
	int kase = 1;
#if defined( xerxes_pc )
	if (!freopen("in1", "r", stdin))
		puts("error opening file in "), assert(0);
	kase = 1;
#endif
	fo(i,kase)
	{
		read();
		proc();
	}
	return 0;
}
inline void read() {
	number_count = fastIn();
	fo(i,number_count)
		numbers[i] = fastIn();
}
inline void proc() {
	build(1, 0, number_count - 1);
	int q = fastIn();
	while (q--) {
		int u = fastIn();
		int v = fastIn();
		Node res = Query(1, 0, number_count - 1, u - 1, v - 1);
		cout << res.bst << '\n';
	}
}
